package com.zdkj.subway.stm.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.annotation.SwStmNoRepeatSubmitBaseController;
import com.zdkj.subway.stm.dvo.SwStCheckDetailsDvo;
import com.zdkj.subway.stm.dvo.SwStReportqueryStationincomeExcelDvo;
import com.zdkj.subway.stm.entity.SwStReportqueryStationincome;
import com.zdkj.subway.stm.mapper.SwStReportqueryStationincomeMapper;
import com.zdkj.subway.stm.reportquery.util.StationSettleUtil;
import com.zdkj.subway.stm.service.SwStReportqueryStationincomeService;
import com.zdkj.subway.stm.util.DateUtilStm;
import com.zdkj.subway.stm.util.TvmCheckDetails;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 车站报表查询
 *
 * @author songtuo
 * @date 2020-07-14 11:52:58
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstreportquery")
@Api(value = "swstreportquery", tags = "车站报表查询")
@Slf4j
public class SwStReportQueryController extends SwStmNoRepeatSubmitBaseController {

    private final SwStReportqueryStationincomeService swStReportqueryStationincomeService;
    @Resource
    SwStReportqueryStationincomeMapper swStReportqueryStationincomeMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
	private static final Logger LOGGER = LoggerFactory.getLogger(SwStReportQueryController.class);

    @ApiOperation(value = "分页查询车站营收日报表", notes = "分页查询车站营收日报表")
    @GetMapping("/page")
    public R getPage(Page page, SwStReportqueryStationincome swStReportqueryStationincome) {
        return R.ok(swStReportqueryStationincomeService.page(
                page, Wrappers.query(swStReportqueryStationincome).lambda().orderByDesc(SwStReportqueryStationincome::getStatisticsDate)
        ));
    }

    /**
     * 批量重新生成车站营收日报表
     *
     * @param param
     * @return R
     */
    @ApiOperation(value = "批量重新生成车站营收日报表", notes = "批量重新生成车站营收日报表")
    @PostMapping({"/refreshBatchStationincome"})
    public R refreshBatchStationincome(@RequestParam Map<String,String> param) {
        String md5 = MD5.create().digestHex(param.toString());
        if (noPass(SecurityUtils.getUser().getId(), "stmBatchStationincome", md5)) {
            return R.failed("请勿频繁操作");
        }
        String lineCodes = param.get("lineCode");
        String startDateStr = param.get("startDateStr");
        String endDateStr = param.get("endDateStr");
        List<String> listDate= swStReportqueryStationincomeService.handleRangeDate(startDateStr,endDateStr);
        boolean flag = false;
        if(CollectionUtil.isNotEmpty(listDate)){
            for (String dateStr : listDate) {
                List<SwStReportqueryStationincome> listDB = swStReportqueryStationincomeService.incomeDailyReport(lineCodes,dateStr);
                if(CollectionUtils.isNotEmpty(listDB)){
                    for (SwStReportqueryStationincome x : listDB) {
                        //物理表的数据
                        LambdaQueryWrapper<SwStReportqueryStationincome> stationincome = Wrappers.<SwStReportqueryStationincome>lambdaQuery()
                                .eq(SwStReportqueryStationincome::getStatisticsDate, x.getStatisticsDate())
                                .eq(SwStReportqueryStationincome::getLineCode, x.getLineCode())
                                .eq(SwStReportqueryStationincome::getDelFlag, "0")
                                .eq(SwStReportqueryStationincome::getStationCode, x.getStationCode());
                        List<SwStReportqueryStationincome> list = swStReportqueryStationincomeService.list(stationincome);
                        //判断物理表是否存在前一天的数据，存在更新（根据线路+站点+日期），人不变
                        if(CollectionUtils.isNotEmpty(list)){
                            SwStReportqueryStationincome beforeOne = list.get(0);
                            x.setId(beforeOne.getId());
                            x.setPassengerAttendName(beforeOne.getPassengerAttendName());
                            x.setPassengerAttendJobNo(beforeOne.getPassengerAttendJobNo());
                            x.setAttendName(beforeOne.getAttendName());
                            x.setAttendJobNo(beforeOne.getAttendJobNo());
                            flag = swStReportqueryStationincomeService.updateById(x);
                            //出现重复多条，直接删除，只保留1条
                            if(list.size() > 1){
                                for (int i = 1; i < list.size(); i++) {
                                    swStReportqueryStationincomeService.remove(Wrappers.query(list.get(i)));
                                }
                            }
                        }else { //不存在新增
                            SwStReportqueryStationincome newStationincome = new SwStReportqueryStationincome();
                            newStationincome.setLineCode(x.getLineCode());
                            newStationincome.setStationCode(x.getStationCode());
                            swStReportqueryStationincomeService.save(x);
                            newStationincome.setId(x.getId());
                            flag = swStReportqueryStationincomeService.updateById(newStationincome);
                        }
                    }
                }
            }
            return R.ok(flag,"重新生成营收日报表成功！");
        }
        return R.failed("重新生成营收日报表失败！");
    }

    /**
     * 车站营收日报表
     * @param statisticsDate  查詢日期 yyyy-MM-dd
     * @return R
     */
    /*@ApiOperation(value = "车站营收日报表查询", notes = "车站营收日报表查询")
    @GetMapping("/stationincome/{statisticsDate}" )
    public R getStationIncome(@PathVariable(value = "statisticsDate") String statisticsDate) {
    	Map mapParam = new HashMap();
    	mapParam.put("lineCode",SecurityUtils.getUser().getLineCode());
    	mapParam.put("stationCode",SecurityUtils.getUser().getStationCode());
    	mapParam.put("statisticsDate", DateUtilStm.stringToDate(statisticsDate, "yyyy-MM-dd"));
        return R.ok(StationSettleUtil.getStationIncomeReport(mapParam));
    }*/

    /**
     * 车站营收日报表
     * @param statisticsDate  查詢日期 yyyy-MM-dd
     * @return R
     */
    @ApiOperation(value = "车站营收日报表查询", notes = "车站营收日报表查询")
    @GetMapping("/stationincome/{statisticsDate}" )
    public R getStationIncome(@PathVariable(value = "statisticsDate") String statisticsDate) {
        PigxUser user = SecurityUtils.getUser();
        LambdaQueryWrapper<SwStReportqueryStationincome> wrapper = new QueryWrapper<SwStReportqueryStationincome>().lambda();
        wrapper.eq(SwStReportqueryStationincome::getLineCode,user.getLineCode());
        wrapper.eq(SwStReportqueryStationincome::getStationCode,user.getStationCode());
        wrapper.eq(SwStReportqueryStationincome::getStatisticsDate,DateUtilStm.LocalDateToString(statisticsDate));
        return R.ok(swStReportqueryStationincomeService.getOne(wrapper));
    }
    /**
     * 车站营收日报表
     * @param statisticsDate  查詢日期 yyyy-MM-dd
     * @return R
     */
    @ApiOperation(value = "车站营收日报表查询", notes = "车站营收日报表查询")
    @GetMapping("/stationincomerealtime/{statisticsDate}" )
    public R getStationIncomeRealTime(@PathVariable(value = "statisticsDate") String statisticsDate) {
        PigxUser user = SecurityUtils.getUser();
        SwStReportqueryStationincome swStReportqueryStationincome = new SwStReportqueryStationincome();
        //判断是否存在的条件：线路+站点+日期
        LambdaQueryWrapper<SwStReportqueryStationincome> wrapper = new QueryWrapper<SwStReportqueryStationincome>().lambda();
        wrapper.eq(SwStReportqueryStationincome::getLineCode,user.getLineCode());
        wrapper.eq(SwStReportqueryStationincome::getStationCode,user.getStationCode());
        wrapper.eq(SwStReportqueryStationincome::getStatisticsDate,DateUtilStm.LocalDateToString(statisticsDate));
        //可能存在多条数据，根据日期倒序获取第一条
        wrapper.orderByDesc(SwStReportqueryStationincome::getStatisticsDate);

        List<SwStReportqueryStationincome> list = swStReportqueryStationincomeService.list(wrapper);
        if(CollectionUtils.isNotEmpty(list)){
            swStReportqueryStationincome = list.get(0);
            swStReportqueryStationincome.setSourceFlag("1");
            //存在多条删除一条
            if(list.size()>1){
                for (int i = 1; i < list.size(); i++) {
                    swStReportqueryStationincomeService.remove(Wrappers.query(list.get(i)));
                }
            }
        }else {
            List<SwStReportqueryStationincome> stationincomeList = StationSettleUtil.calcStationIncomeDailyReport(user.getLineCode(),user.getStationCode(),statisticsDate);
            if(stationincomeList.size()>0){
                swStReportqueryStationincome = stationincomeList.get(0);
                swStReportqueryStationincome.setSourceFlag("2");
            }
        }
        return R.ok(swStReportqueryStationincome);

    }
    /**
     * 车站营收日报表按周期查询
     * @param startTime  开始日期 yyyy-MM-dd
     * @param endTime  结束日期 yyyy-MM-dd
     * @return R
     */
    @ApiOperation(value = "车站营收日报表按周期查询", notes = "车站营收日报表按周期查询")
    @GetMapping("/stationincomerealtime" )
    public R getStationIncomeRealTime(String startTime,String endTime) throws Exception {
        //单独加方法，根据开始日期，结束结束，根据日期范围，在物理表内查找没在里面的日期，选择的结束日期大于当前日期，return
        LocalDate now = LocalDate.now();
        LocalDate endDate = DateUtilStm.LocalDateToString(endTime);
        if(endDate.isAfter(now)){
            return R.failed("结束日期大于当前日期！");
        }
        PigxUser user = SecurityUtils.getUser();
        String lineCode = user.getLineCode();
        String stationCode = user.getStationCode();
        SwStReportqueryStationincome swStReportqueryStationincome = new SwStReportqueryStationincome();
        List<SwStReportqueryStationincome> reportqueryStationincome = swStReportqueryStationincomeMapper.getStationIncome(lineCode,stationCode,startTime,endTime);
        List<String> dateMap = swStReportqueryStationincomeMapper.dateMap(lineCode,stationCode,startTime,endTime);
        List<String> datesBetweenTwoDate = DateUtilStm.getDatesBetweenTwoDate(startTime, endTime);
        //过滤出不存在的日期
        List<String> collect = datesBetweenTwoDate.stream().filter(it -> !dateMap.contains(it)).collect(Collectors.toList());
        for (String date : collect) {
            List<SwStReportqueryStationincome> incomes = StationSettleUtil.calcStationIncomeDailyReport(lineCode, stationCode, date);
            if(CollectionUtil.isNotEmpty(incomes)){
                reportqueryStationincome.get(0).setCashBox(reportqueryStationincome.get(0).getCashBox().add(incomes.get(0).getCashBox()));
                reportqueryStationincome.get(0).setSupplment(reportqueryStationincome.get(0).getSupplment().add(incomes.get(0).getSupplment()));
                reportqueryStationincome.get(0).setHand(reportqueryStationincome.get(0).getHand().add(incomes.get(0).getHand()));
                reportqueryStationincome.get(0).setSubTvmTotal(reportqueryStationincome.get(0).getSubTvmTotal().add(incomes.get(0).getSubTvmTotal()));
                reportqueryStationincome.get(0).setMarketTicket(reportqueryStationincome.get(0).getMarketTicket().add(incomes.get(0).getMarketTicket()));
                reportqueryStationincome.get(0).setYzdcp(reportqueryStationincome.get(0).getYzdcp().add(incomes.get(0).getYzdcp()));
                reportqueryStationincome.get(0).setYzdcp(reportqueryStationincome.get(0).getYzdcp().add(incomes.get(0).getYzdcp()));
                reportqueryStationincome.get(0).setYjzp(reportqueryStationincome.get(0).getYjzp().add(incomes.get(0).getYjzp()));
                reportqueryStationincome.get(0).setSubBomTotal(reportqueryStationincome.get(0).getSubBomTotal().add(incomes.get(0).getSubBomTotal()));
                reportqueryStationincome.get(0).setConductorShort(reportqueryStationincome.get(0).getConductorShort().add(incomes.get(0).getConductorShort()));
                reportqueryStationincome.get(0).setAddMoney(reportqueryStationincome.get(0).getAddMoney().add(incomes.get(0).getAddMoney()));
                reportqueryStationincome.get(0).setConductorLong(reportqueryStationincome.get(0).getConductorLong().add(incomes.get(0).getConductorLong()));
                reportqueryStationincome.get(0).setOther(reportqueryStationincome.get(0).getOther().add(incomes.get(0).getOther()));
                reportqueryStationincome.get(0).setTotal(reportqueryStationincome.get(0).getTotal().add(incomes.get(0).getTotal()));
            }
        }
        if(CollectionUtil.isNotEmpty(reportqueryStationincome)){
            swStReportqueryStationincome = reportqueryStationincome.get(0);
        }
        return R.ok(swStReportqueryStationincome);

    }

    /**
     * 新增/更新车站营收日报表
     *
     * @param stationincome
     * @return R
     */
    @ApiOperation(value = "新增/更新车站营收日报表", notes = "新增/更新车站营收日报表")
    @PostMapping({"/saveOrUpdateStationincome"})
    public R save(@RequestBody SwStReportqueryStationincome stationincome) {
        if("1".equals(stationincome.getSourceFlag())){//sourceFlag=1时，直接更新库存日报表的人员信息
            //判断是否存在的条件：线路+站点+日期
            LambdaQueryWrapper<SwStReportqueryStationincome> newStationincome = Wrappers.<SwStReportqueryStationincome>lambdaQuery()
                    .eq(SwStReportqueryStationincome::getLineCode, stationincome.getLineCode())
                    .eq(SwStReportqueryStationincome::getStationCode, stationincome.getStationCode())
                    .eq(SwStReportqueryStationincome::getDelFlag, "0")
                    .eq(SwStReportqueryStationincome::getStatisticsDate, stationincome.getStatisticsDate());
            SwStReportqueryStationincome newServiceOne = swStReportqueryStationincomeService.getOne(newStationincome);

            if(ObjectUtil.isNotEmpty(newServiceOne)){
                newServiceOne.setPassengerAttendName(stationincome.getPassengerAttendName());
                newServiceOne.setPassengerAttendJobNo(stationincome.getPassengerAttendJobNo());
                newServiceOne.setAttendName(stationincome.getAttendName());
                newServiceOne.setAttendJobNo(stationincome.getAttendJobNo());
                swStReportqueryStationincomeService.updateById(newServiceOne);
            }
        }else {//sourceFlag=2时，根据前台界面所有数据保存到库存日报表
            swStReportqueryStationincomeService.save(stationincome);
        }
        return R.ok();
    }

    /**
     * 重新生成车站营收日报表
     *
     * @param statisticsDate
     * @return R
     */
    @ApiOperation(value = "重新生成车站营收日报表", notes = "重新生成车站营收日报表")
    @PostMapping({"/refreshStationincome"})
    public R refreshStationincome(String statisticsDate) {
        String md5 = MD5.create().digestHex(statisticsDate);
        if (noPass(SecurityUtils.getUser().getId(), "stmStationincome", md5)) {
            return R.failed("请勿频繁操作");
        }
        PigxUser user = SecurityUtils.getUser();
        //判断是否存在的条件：线路+站点+日期
        LambdaQueryWrapper<SwStReportqueryStationincome> oldStationincome = Wrappers.<SwStReportqueryStationincome>lambdaQuery()
                .eq(SwStReportqueryStationincome::getLineCode, user.getLineCode())
                .eq(SwStReportqueryStationincome::getStationCode, user.getStationCode())
                .eq(SwStReportqueryStationincome::getDelFlag, "0")
                .eq(SwStReportqueryStationincome::getStatisticsDate, DateUtilStm.LocalDateToString(statisticsDate));
        SwStReportqueryStationincome oldServiceOne = swStReportqueryStationincomeService.getOne(oldStationincome);

        //判断库存日报表如果存在更新，但不更新人员信息   组织最新数据
        SwStReportqueryStationincome newServiceOne = StationSettleUtil.
                calcStationIncomeDailyReport(user.getLineCode(),
                        user.getStationCode(),
                        statisticsDate).get(0);

        if(ObjectUtil.isNotEmpty(oldServiceOne)){
            newServiceOne.setId(oldServiceOne.getId());
			String passengerAttentName = oldServiceOne.getPassengerAttendName();
            if(StringUtils.isEmpty(passengerAttentName)){
            	passengerAttentName = newServiceOne.getPassengerAttendName();
			}
            String passengerAttentJobNo = oldServiceOne.getPassengerAttendJobNo();
            if(StringUtils.isEmpty(passengerAttentJobNo)){
            	passengerAttentJobNo = newServiceOne.getPassengerAttendJobNo();
			}
            String attentName = oldServiceOne.getAttendName();
            if(StringUtils.isEmpty(attentName)){
            	attentName = newServiceOne.getAttendName();
			}
            String attendJobNo = oldServiceOne.getAttendJobNo();
            if(StringUtils.isEmpty(attendJobNo)){
            	attendJobNo = newServiceOne.getAttendJobNo();
			}
            newServiceOne.setPassengerAttendName(passengerAttentName);
            newServiceOne.setPassengerAttendJobNo(passengerAttentJobNo);
            newServiceOne.setAttendName(attentName);
            newServiceOne.setAttendJobNo(attendJobNo);
			newServiceOne.setCreateTime(oldServiceOne.getCreateTime());
        }
        //存在更新，不存在新增
        return R.ok(swStReportqueryStationincomeService.saveOrUpdate(newServiceOne));
    }



    /**
     * 审核模块修改钱箱未卸载入账日期重新生成车站营收日报表
     *
     * @param statisticsDate
     * @return R
     */
    @ApiOperation(value = "审核模块修改钱箱未卸载入账日期重新生成车站营收日报表", notes = "审核模块修改钱箱未卸载入账日期重新生成车站营收日报表")
    @Inner
    @PostMapping("/refreshStationOfIncome")
    public R refreshStationOfIncome(String lineCode, String stationCode, String statisticsDate,String from) {
        //判断是否存在的条件：线路+站点+日期
        LambdaQueryWrapper<SwStReportqueryStationincome> oldStationincome = Wrappers.<SwStReportqueryStationincome>lambdaQuery()
                .eq(SwStReportqueryStationincome::getLineCode, lineCode)
                .eq(SwStReportqueryStationincome::getStationCode, stationCode)
                .eq(SwStReportqueryStationincome::getDelFlag, "0")
                .eq(SwStReportqueryStationincome::getStatisticsDate, DateUtilStm.LocalDateToString(statisticsDate));
        SwStReportqueryStationincome oldServiceOne = swStReportqueryStationincomeService.getOne(oldStationincome);
        SwStReportqueryStationincome newServiceOne = new SwStReportqueryStationincome();
        if(ObjectUtil.isNotEmpty(oldServiceOne)){
            SwStReportqueryStationincome income = StationSettleUtil.getStationIncomeReport(oldServiceOne);
            newServiceOne.setId(oldServiceOne.getId());
            newServiceOne.setCashBox(income.getCashBox());
            newServiceOne.setTotal(income.getTotal());
            return R.ok(swStReportqueryStationincomeService.updateById(newServiceOne));
        }
        return R.failed();
    }

	 /**
     * 审核模块修改钱箱未卸载入账日期重新生成车站营收日报表
     *
     * @param statisticsDate
     * @return R
     */
    @ApiOperation(value = "审核模块修改钱箱未卸载入账日期重新生成车站营收日报表", notes = "审核模块修改钱箱未卸载入账日期重新生成车站营收日报表")
    @PostMapping("/refreshStationOfIncomeByBox")
    public R refreshStationOfIncomeByBox(String lineCode, String stationCode, String statisticsDate) {
        //判断是否存在的条件：线路+站点+日期
        LambdaQueryWrapper<SwStReportqueryStationincome> oldStationincome = Wrappers.<SwStReportqueryStationincome>lambdaQuery()
                .eq(SwStReportqueryStationincome::getLineCode, lineCode)
                .eq(SwStReportqueryStationincome::getStationCode, stationCode)
                .eq(SwStReportqueryStationincome::getDelFlag, "0")
                .eq(SwStReportqueryStationincome::getStatisticsDate, DateUtilStm.LocalDateToString(statisticsDate));
        SwStReportqueryStationincome oldServiceOne = swStReportqueryStationincomeService.getOne(oldStationincome);
        SwStReportqueryStationincome newServiceOne = new SwStReportqueryStationincome();
        if(ObjectUtil.isNotEmpty(oldServiceOne)){
            SwStReportqueryStationincome income = StationSettleUtil.getStationIncomeReport(oldServiceOne);
            newServiceOne.setId(oldServiceOne.getId());
            newServiceOne.setCashBox(income.getCashBox());
            newServiceOne.setTotal(income.getTotal());
            return R.ok(swStReportqueryStationincomeService.updateById(newServiceOne));
        }
        return R.failed();
    }


    @ApiOperation(value = "定时任务统计车站日报表", notes = "每天早晨9点定时统计前一天车站营收日报表数据")
    @Inner
    @PostMapping("/calcStationIncomeDailyReport")
    public R calcIncomeDailyReport() {  
		LOGGER.info("车站营收日报表定时任务开始执行时间===="+LocalDateTime.now());
		String dateTime = DateUtilStm.asPreDateString(LocalDateTime.now());
		//定时生成组织的数据
		List<SwStReportqueryStationincome> listDB = swStReportqueryStationincomeService.calcStationIncomeDailyReport(dateTime);
        List<SwStReportqueryStationincome> objects = new ArrayList<>();

		if(CollectionUtils.isNotEmpty(listDB)){
            listDB.forEach(x -> {
                //物理表的数据
                LambdaQueryWrapper<SwStReportqueryStationincome> stationincome = Wrappers.<SwStReportqueryStationincome>lambdaQuery()
                        .eq(SwStReportqueryStationincome::getStatisticsDate, x.getStatisticsDate())
                        .eq(SwStReportqueryStationincome::getLineCode, x.getLineCode())
                        .eq(SwStReportqueryStationincome::getDelFlag, "0")
                        .eq(SwStReportqueryStationincome::getStationCode, x.getStationCode());
                SwStReportqueryStationincome beforeOne = swStReportqueryStationincomeService.getOne(stationincome);
                //判断物理表是否存在前一天的数据，存在更新（根据线路+站点+日期），人不变
                if(ObjectUtil.isNotEmpty(beforeOne)){
                    x.setId(beforeOne.getId());
                    x.setPassengerAttendName(beforeOne.getPassengerAttendName());
                    x.setPassengerAttendJobNo(beforeOne.getPassengerAttendJobNo());
                    x.setAttendName(beforeOne.getAttendName());
                    x.setAttendJobNo(beforeOne.getAttendJobNo());
                    objects.add(x);
                }else { //不存在新增
                    objects.add(x);
                }
            });
        }
		boolean isOk = swStReportqueryStationincomeService.saveOrUpdateBatch(objects);
		LOGGER.info("车站营收日报表定时任务结束执行时间===="+LocalDateTime.now());
        return R.ok(isOk);
		
    }

    /**
     * 通过id查询车站营收日报表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询车站营收日报表", notes = "通过id查询车站营收日报表")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swStReportqueryStationincomeService.getById(id));
    }
//    /**
//     * 分页查询
//     *
//     * @param page                         分页对象
//     * @param swStReportqueryStationincome 车站营收日报表查询
//     * @return
//     */
//    @ApiOperation(value = "分页查询", notes = "分页查询")
//    @GetMapping("/page")
//    public R getSwStReportqueryStationincomePage(Page page, SwStReportqueryStationincome swStReportqueryStationincome) {
//        //是否是线网人员
//        if (!SecurityUtils.getUser().getBelongToLine()) {
//            QueryWrapper<SwStReportqueryStationincome> queryWrapper = Wrappers.query(swStReportqueryStationincome);
//            queryWrapper.lambda().eq(SwStReportqueryStationincome::getLineCode, SecurityUtils.getUser().getLineCode());
//            queryWrapper.lambda().eq(SwStReportqueryStationincome::getStationCode, SecurityUtils.getUser().getStationCode());
//            return R.ok(swStReportqueryStationincomeService.page(page, queryWrapper));
//        }
//        return R.ok(swStReportqueryStationincomeService.page(page, Wrappers.query(swStReportqueryStationincome)));
//    }
//
//   
//

//   
//
//    /**
//     * 修改车站营收日报表
//     *
//     * @param swStReportqueryStationincome
//     * @return R
//     */
//    @ApiOperation(value = "修改车站营收日报表", notes = "修改车站营收日报表")
//    @SysLog("修改车站营收日报表")
//    @PutMapping
//    @PreAuthorize("@pms.hasPermission('stm_sw_st_reportquery_stationincome_edit')")
//    public R updateById(@RequestBody SwStReportqueryStationincome swStReportqueryStationincome) {
//        return R.ok(swStReportqueryStationincomeService.updateById(swStReportqueryStationincome));
//    }
//
//    /**
//     * 通过id删除车站营收日报表
//     *
//     * @param id id
//     * @return R
//     */
//    @ApiOperation(value = "通过id删除车站营收日报表", notes = "通过id删除车站营收日报表")
//    @SysLog("通过id删除车站营收日报表")
//    @DeleteMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('stm_sw_st_reportquery_stationincome_del')")
//    public R removeById(@PathVariable Integer id) {
//        return R.ok(swStReportqueryStationincomeService.removeById(id));
//    }
     /**
     * 按模版内容导出车站营收日报表
     * @param response
     */
    @PostMapping("/exportByPageContent")
    @SysLog("导出车站营收日报表")
    public void exportByPageContent(@RequestBody SwStReportqueryStationincomeExcelDvo swStReportqueryStationincomeExcelDvo, HttpServletResponse response) {
        swStReportqueryStationincomeService.exportByPageContent(swStReportqueryStationincomeExcelDvo,response);
    }
    /**
     * 车站营收日报表按日期范围导出
     * @param response
     */
    @PostMapping("/exportByPageContentForCycle")
    @SysLog("车站营收日报表按日期范围导出")
    public void exportByPageContentForCycle(@RequestBody SwStReportqueryStationincomeExcelDvo swStReportqueryStationincomeExcelDvo, HttpServletResponse response) {
        swStReportqueryStationincomeService.exportByPageContentForCycle(swStReportqueryStationincomeExcelDvo,response);
    }


    /**
     * 车站TVM钱箱清点记录表
     * @param earningDate  收益日期 yyyy-MM-dd
     * @return R
     */
    @ApiOperation(value = "车站TVM钱箱清点记录表", notes = "车站TVM钱箱清点记录表")
    @GetMapping("/swStCheckDetails/{earningDate}" )
    public R getswStCheckDetails(@PathVariable(value = "earningDate") String earningDate) {

        PigxUser user = SecurityUtils.getUser();
        SwStCheckDetailsDvo swStCheckDetailsDvo = new SwStCheckDetailsDvo();
        swStCheckDetailsDvo.setEarningDate(earningDate);
        swStCheckDetailsDvo.setLineCode(user.getLineCode());
        swStCheckDetailsDvo.setStationCode(user.getStationCode());
        List<Map<String, Object>> swStCheckCkdate =  swStReportqueryStationincomeService.getswStCheckCkData(swStCheckDetailsDvo);
        for (Map<String, Object> item :swStCheckCkdate){
            swStCheckDetailsDvo.setBeginTime(String.valueOf(item.get("begin_time")));
            swStCheckDetailsDvo.setEndTime(String.valueOf(item.get("end_time")));
        }
        List<SwStCheckDetailsDvo> swStCheckDetailsList =  swStReportqueryStationincomeService.getswStCheckDetails(swStCheckDetailsDvo);

        //计算合计和清点金额
        List<SwStCheckDetailsDvo> list = TvmCheckDetails.countCheckDetails(swStCheckDetailsList);
        return R.ok(list);
    }


    /**
     * 导出车站TVM钱箱清点记录表
     * @param earningDate
     * @param response
     */
    @GetMapping("/exportCheckDetails/{earningDate}")
    @ApiOperation(value = "导出车站TVM钱箱清点记录表", notes = "导出车站TVM钱箱清点记录表")
    public void exportCheckDetails(@PathVariable(value = "earningDate") String earningDate, HttpServletResponse response) {

        PigxUser user = SecurityUtils.getUser();
        SwStCheckDetailsDvo swStCheckDetailsDvo = new SwStCheckDetailsDvo();
        swStCheckDetailsDvo.setEarningDate(earningDate);
        swStCheckDetailsDvo.setLineCode(user.getLineCode());
        swStCheckDetailsDvo.setStationCode(user.getStationCode());
        swStReportqueryStationincomeService.exportCheckDetails(swStCheckDetailsDvo,response);
    }

    @Override
    public RedisTemplate<String, String> redisTemplate() {
        return redisTemplate;
    }
}
